package org.exam;

import org.exam.util.ExcelUtil;
import org.exam.util.JDBCUtil;
import com.google.gson.Gson;
import org.exam.entry.Information;
import org.exam.entry.PaperEntry;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
/**
 * 论文主类
 *
 */
public class PaperMain {

    //启动主方法
    public static void main(String[] args) throws SQLException {
        //调用start1 爬取方法，爬取并存入500条 增加
//        String start = start(50);
//        System.out.println(start);

        //调用start2 爬取方法，爬取并存入500条 增加
        //start2(50);
        //JDBCUtil.close();

        //查询
        //JDBCUtil.select(JDBCUtil.getCon());
        //JDBCUtil.close();

        //更改
        //JDBCUtil.update(JDBCUtil.getCon(),622,"计算机概论");
        //JDBCUtil.close();


        //删除
        //JDBCUtil.delete(JDBCUtil.getCon(),200);
        //JDBCUtil.close();

        //导入
//        ExcelUtil.excelImport();

        //导出
        ExcelUtil.export(JDBCUtil.select(JDBCUtil.getCon()));


    }


    /**
     * 爬取方法stat
     *
     * @param num 要爬取的数量
     * @return 返回爬取的状态
     */
    public static String start(Integer num) {
        if (num > 10000 || num < 0) {
            return "爬取数量不合法";
        }
        //爬取的数量
        int num1 = 0;

        String url = "https://dblp.uni-trier.de/search/publ/inc?q=computer&s=ydvspc&h=30&b=";

        try {
            //外层循环爬取页数
            for (int i = 1; i < 4000; i++) {

                // 连接到网站并获取HTML文档
                Document document = Jsoup.connect(url + i).get();

                // 提取标题信息
                Elements titles = document.select("ul>li>.tts-content");

                //循环爬取到的节点
                for (Element title : titles) {
                    Elements authors = title.select("span>a>span");
                    //如果作者大于3个，跳过爬取
                    if (authors.size() > 3) continue;
                    //循环输出每一个作者
                    StringBuilder sb = new StringBuilder();
                    for (Element author : authors) {
                        if (sb.length() > 0) {
                            sb.append("," + author.text());
                        } else {
                            sb.append(author.text());
                        }
                        //存入数据库
                        System.out.println(author.text());
                    }
                    String titleStr = title.select(".title").text();
                    System.out.println("-------------------------------------");
                    //存入数据库
                    JDBCUtil.insert(JDBCUtil.getCon(), titleStr, sb.toString());
                    num1++;
                    //当爬取数量等于输入的数量时，结束爬取
                    if (num1 == num) {
                        return "完成" + num1;
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
        return "成功";
    }


    //爬取网站2
    public static String start2(Integer num) {
        //数量不合法
        if (num>10000 || num<0){
            return "数量不合法";
        }
        //成功爬取条数
        Integer number=0;
        //爬取的网站地址
        String apiUrl = "https://www.zhangqiaokeyan.com/solorscience/docsearchlist.html?types=1_2_3_4_5_6_7_9_0_4-0&sertexts=%E8%AE%A1%E7%AE%97%E6%9C%BA&options=200&links=&sort=&pageSize=20&currentPage=";

        try {
            for (int i = 1; i < 6000; i++) {
                // 创建 URL 对象
                URL url = new URL(apiUrl+i);
                // 打开连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                // 设置请求方式为 POST
                connection.setRequestMethod("POST");
                connection.setDoOutput(true); // 允许输出

                // 设置请求头
                connection.setRequestProperty("Content-Type", "application/json");

                // 获取响应码
                int responseCode = connection.getResponseCode();

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // 读取响应内容
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }

                    // 处理响应内容
                    System.out.println(response.toString());
                    //转成list
                    // 使用 Gson 将字符串转换为 JSON 对象
                    Gson gson = new Gson();
                    PaperEntry paper = gson.fromJson(response.toString(), PaperEntry.class);
                    System.out.println(paper.getData().getDocList().size());
                    for (Information information : paper.getData().getDocList()) {

                        //过滤没有作者的垃圾信息
                        if (information.getF_author().size() == 0) {
                            continue;
                        }
                        //循环作者
                        StringBuilder sb = new StringBuilder();
                        for (String author : information.getF_author()) {
                            if (sb.length() == 0) {
                                sb.append(author);
                            } else {
                                sb.append("," + author);
                            }
                            System.out.println(author);
                        }
                        String f_title = information.getF_title();
                        //第一次出现的索引
                        int firstIndex = f_title.indexOf("<", 0);
                        int secondIndex = f_title.indexOf(">", 0);
                        //System.out.println(information.getF_title());
                        StringBuilder titleSb = new StringBuilder(f_title);
                        //过滤无用样式
                        titleSb.delete(firstIndex, secondIndex + 1);
                        firstIndex = titleSb.indexOf("<", 0);
                        secondIndex = titleSb.indexOf(">", 0);
                        //过滤无用样式
                        titleSb.delete(firstIndex, secondIndex + 1);
                        System.out.println(titleSb.toString());
                        //爬取数据添加到数据库
                        JDBCUtil.insert(JDBCUtil.getCon(),titleSb.toString(),sb.toString());
                        System.out.println("---------------------");
                        number++;
                        if (number==num){
                            return "成功爬取条数："+number;
                        }
                    }
                    // 关闭连接
                    connection.disconnect();
                } else {
                    System.out.println("HTTP request failed with response code: " + responseCode);
                }
            }

            } catch(IOException e){
                e.printStackTrace();
            } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return "失败";
    }

}
